package com.mfm;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Stream终止操作
 * @Auther: MFM
 * @Date: 2022/3/1 - 03 - 01 - 15:54
 * @version: 1.0
 */

public class StreamTest2 {
    class student{
        int id;
        String name;
        int age;

        @Override
        public String toString() {
            return "student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            student student = (student) o;
            return id == student.id && age == student.age && Objects.equals(name, student.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, name, age);
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public student() {
        }

        public student(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public student(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public student(int id) {
            this.id = id;
        }
    }

    //匹配与查找
    public void test1(){
        List<StreamTest2.student> list = new ArrayList<>();
        list.add(new StreamTest2.student(1,"zhangsan",12));
        list.add(new StreamTest2.student(2,"lisi",14));
        list.add(new StreamTest2.student(3,"wangwu",10));
        list.add(new StreamTest2.student(4,"maliu",9));
        list.add(new StreamTest2.student(5,"huqi",11));
        list.add(new StreamTest2.student(5,"huqi",11));

        //allMatch(Predicate p)--检查是否匹配所有元素
        boolean b = list.stream().allMatch(s -> s.getAge() > 3);
        System.out.println(b);

        //anyMatch(Predicate p)--检查是否存在符合条件的元素
        boolean b1 = list.stream().anyMatch(s -> s.getId() == 1);
        System.out.println(b1);

        //noneMatch(Predicate p)--检查是否没有匹配的元素
        boolean b2 = list.stream().noneMatch(s -> s.getName().startsWith("zhang"));
        System.out.println(b2);

        //findFirst--返回第一个元素
        Optional<student> first = list.stream().findFirst();
        System.out.println(first);

        //findAny--返回当前流中任意元素
        Optional<student> any = list.stream().findAny();
        System.out.println(any);

        //count--返回流中总个数    返回值为long
        long count = list.stream().filter(e -> e.getAge()>3).count();
        System.out.println(count);

        //max(Comparator com)--返回流中最大值


        //min(Comparator com)--返回流中最小值

        //forEach(Consumer c)--内部迭代
    }

    //规约
    public void test2(){
        List<StreamTest2.student> list = new ArrayList<>();
        list.add(new StreamTest2.student(1,"zhangsan",12));
        list.add(new StreamTest2.student(2,"lisi",14));
        list.add(new StreamTest2.student(3,"wangwu",10));
        list.add(new StreamTest2.student(4,"maliu",9));
        list.add(new StreamTest2.student(5,"huqi",11));
        list.add(new StreamTest2.student(5,"huqi",11));
        //reduce(T identity(初始值), BinaryOperator)--可以将流中元素反复结合起来，得到一个值。返回T
        List<Integer> list1 = Arrays.asList(1,2,3,4,5,6,7,8);
        Integer reduce = list1.stream().reduce(0, Integer::sum);
        System.out.println(reduce);
        System.out.println();

        //reduce(BinaryOperator)--可以将流中元素反复结合起来，得到一个值。返回Optional<T>
        Optional<Integer> reduce1 = list.stream().map(student::getAge).reduce(Integer::sum);
        //Optional<Integer> reduce1 = list.stream().map(student::getAge).reduce((a1,a2) -> a1+a2);
        System.out.println(reduce1);
    }

    //收集
    public void test3(){
        List<StreamTest2.student> list = new ArrayList<>();
        list.add(new StreamTest2.student(1,"zhangsan",12));
        list.add(new StreamTest2.student(2,"lisi",14));
        list.add(new StreamTest2.student(3,"wangwu",10));
        list.add(new StreamTest2.student(4,"maliu",9));
        list.add(new StreamTest2.student(5,"huqi",11));
        list.add(new StreamTest2.student(5,"huqi",11));

        //collect(Collect c)--将流转换为其他形式。接收一个Collector接口的实现方式，用于给Stream中的元素做汇总的方法
        List<student> collect = list.stream().filter(s -> s.getAge() > 10).collect(Collectors.toList());
        System.out.println(collect);
        System.out.println();

        Set<student> collect1 = list.stream().filter(s -> s.getAge() > 10).collect(Collectors.toSet());
        System.out.println(collect1);
    }

    public static void main(String[] args) {
        new StreamTest2().test3();
    }
}
